package com.sinoeyes.compent.service.impl;

/**
 * @program: PerformanceX
 * @description: 客户集service
 * @author: liu yan
 * @create: 2019/09/26 15:02
 */

import com.sinoeyes.common.constant.Constants;
import com.sinoeyes.common.result.RCode;
import com.sinoeyes.common.utils.DateUtils;
import com.sinoeyes.common.utils.StringUtils;
import com.sinoeyes.compent.mapper.CustomerMapper;
import com.sinoeyes.compent.po.Customer;
import com.sinoeyes.compent.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户集模块Service业务层处理
 *
 * @author aries.hao
 * @date 2019-09-26
 */
@Service
public class CustomerServiceImpl implements ICustomerService {

    private final CustomerMapper customerMapper;

    @Autowired
    public CustomerServiceImpl(CustomerMapper customerMapper) {
        this.customerMapper = customerMapper;
    }

    /**
     * 查询客户集模块
     *
     * @param id 客户集模块ID
     * @return 客户集模块
     */
    @Override
    public Customer selectCustomerById(String id) {
        return customerMapper.selectCustomerById(id);
    }

    /**
     * 查询客户集模块列表
     *
     * @param customer 客户集模块
     * @return 客户集模块
     */
    @Override
    public List<Customer> selectCustomerList(Customer customer) {
        return customerMapper.selectCustomerList(customer);
    }

    /**
     * 新增客户集模块
     *
     * @param customer 客户集模块
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int insertCustomer(Customer customer) {
        return customerMapper.insertCustomer(customer);
    }

    /**
     * 修改客户集模块
     *
     * @param customer 客户集模块
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int updateCustomer(Customer customer) {
        return customerMapper.updateCustomer(customer);
    }

    /**
     * 删除客户集模块对象
     *
     * @param id 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public RCode deleteCustomerByIds(String id, String strFlag) {
        int idLen;
        String[] ids = id.split(",");
        if ((idLen = ids.length) == 0) {
            return RCode.IMPORT_ERROR;
        }

//        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        Customer customer = new Customer();
        customer.setId(id);
        if (!StringUtils.isBlank(strFlag)) {
            // 启用
            if (Constants.ENABlE_FLAG.equals(strFlag)) {
                if (idLen > 1) {
                    return new RCode(customerMapper.enableCustomerInfoByIds(customer), "");
                }
                return new RCode(customerMapper.enableCustomerInfoById(customer), "");
            }

            // 废用
            if (Constants.DEL_FLAG.equals(strFlag)) {
                if (idLen > 1) {
                    return new RCode(customerMapper.deleteCustomerInfoByIds(customer), "");
                }
                return new RCode(customerMapper.deleteCustomerInfoById(customer), "");
            }
        }
        return RCode.NO_QUERY;
    }
}

